// pages/detail/detail.js
import { Home } from '../home/home-model.js';
var home = new Home(); //实例化 首页 对象

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info_id:'',
    username:'',
    head:'',
    time:'',
    title:'',
    content:'',
    imgList:[],
    name: '',
    mobile: '',
    address: '',
    feeType:1,
    money:'',
    comments:[],
    showadpot:true,
    adpot_id:0
  },
  doComment:function(){
    wx.navigateTo({
      url: '../../pages/comment/comment?id=' + this.data.info_id,
    })
  },
  sortComment:function(){
    var comments = this.data.comments;
    comments.reverse();
    that.setData({
      comments: comments
    });
  },
  //采纳
  adopt:function(e){
    var session_key = wx.getStorageSync('session_key');
    if (session_key == '') {
      wx.showToast({
        title: '没有登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    var comment_id=e.target.id;
    var data = {
      "session_key": session_key,
      'info_id': this.data.info_id,
      "comment_id": comment_id,
    }

    home.submitAdopt(data, (res) => {
      if (res.status == 1) {
        wx.showToast({
          title: '采纳成功',
          icon: 'success',
          duration: 2000
        })
        this.setData({
          showadpot: false,
          adpot_id: comment_id
        });
      } else {
        wx.showToast({
          title: res.info,
          icon: 'none',
          duration: 2000
        });
      }
    });
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    var that = this;
    home.getInfoDetail(id,(data) => {
      if(data.status){
        var info = data.info;
        var head = (info.head == '') ? '../../images/myCenter/head.jpeg' : info.head;
        that.setData({
          info_id: id,
          username: info.username,
          head: head,
          time: info.time,
          title: info.title,
          content: info.content,
          imgList: info.imgList,
          name: info.name,
          mobile: info.mobile,
          address: info.address,
          feeType: info.feeType,
          money: info.money,
          showadpot: info.showadpot,
          comments: info.comments,
          adpot_id: info.adpot_id
        });
      }
    });

  },

  imgShow:function(e){
    var src = e.target.dataset.src;//获取data-src
    var imgList = e.target.dataset.list; //获取data-list
    wx.previewImage({
        current: src, // 当前显示图片的http链接
        urls: imgList // 需要预览的图片http链接列表
      })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})